package com.ruoyi.project.system.controller;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginBody;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.SysLoginService;
import com.ruoyi.framework.security.service.SysPermissionService;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysMenu;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法(原)
     *
     * @param loginBody 登陆信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        Integer tenantId = loginBody.getTenantId();
        System.out.println("controller:"+loginBody.getTenantId());
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        if(StringUtils.isNull(tenantId)){
            //租户id为null,进行普通用户登陆
            System.out.println("进行系统管理用户登陆");
            String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                    loginBody.getUuid());
            ajax.put(Constants.TOKEN, token);
            ajax.put("type","admin");
            return ajax;
        }else{
            //租户id不为null,进行租户登陆
            System.out.println("进行租户用户登陆");
            String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                    loginBody.getUuid(),loginBody.getTenantId());
            ajax.put(Constants.TOKEN, token);
            ajax.put("type","tenant");
            return ajax;
        }
    }

    /**
     * 登录方法(租户id)
     * 
     * @param loginBody 登陆信息
     * @return 结果
     */
    @PostMapping("/loginTenants")
    public AjaxResult loginTenants(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        loginBody.getTenantId();

        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid(),loginBody.getTenantId());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 多租户登陆
     *
     * @param loginBody
     * @return
     */
//    @PostMapping("/loginTenants")
//    public AjaxResult loginTenants(@RequestBody LoginBody loginBody){
//        System.out.println("loginBody:"+loginBody.toString());
//        AjaxResult ajax = AjaxResult.success();
//        //生成令牌
//        String tenantToken = loginService.login(loginBody.getUsername(),loginBody.getPassword(),loginBody.getCode(),
//                loginBody.getUuid(),loginBody.getTenantId());
//        ajax.put(Constants.TENANT_TOKEN,tenantToken);
//        return ajax;
//    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
